package util;

import android.app.Activity;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.beaglebuddy.mp3.MP3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/1/21.
 */

public class AudioParse {

    private Activity activity;
    private WebView webView;
    private Handler handler;
    private boolean state;

    public AudioParse(Activity activity, WebView webView, Handler handler) {
        this.activity = activity;
        this.webView = webView;
        this.handler = handler;
    }

    public Activity getActivity() {
        return activity;
    }

    @JavascriptInterface
    public void scanAllAudioFiles(){
        //生成动态数组，并且转载数据
        ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();

        //查询媒体数据库
        Cursor cursor = activity.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        //遍历媒体数据库
        if(cursor.moveToFirst()){

            while (!cursor.isAfterLast()) {

                //歌曲编号
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                //歌曲标题
                String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                //歌曲的专辑名：MediaStore.Audio.Media.ALBUM
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                //歌曲的歌手名： MediaStore.Audio.Media.ARTIST
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                //歌曲文件的路径 ：MediaStore.Audio.Media.DATA
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                //歌曲的总播放时长 ：MediaStore.Audio.Media.DURATION
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                //歌曲文件的大小 ：MediaStore.Audio.Media.SIZE
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

                if(size>1024*800){//大于800K
//                    HashMap<String, Object> map = new HashMap<String, Object>();
//                    map.put("musicId", id);
//                    map.put("musicTitle", tilte);
//                    map.put("musicAlbum", album);
//                    map.put("musicArtist", artist);
//                    map.put("musicFileUrl", url);
//                    map.put("musicDuration", duration);
//                    map.put("music_file_name", tilte);
//                    mylist.add(map);
                    try {
                        MP3 mp3 = new MP3(url);
                        if (!mp3.hasErrors()){
                            Message msg = new Message();
                            msg.obj = mp3;
                            handler.sendMessage(msg);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                cursor.moveToNext();
            }
            state = true;
        }
    }

    @JavascriptInterface
    public boolean isState() {
        return state;
    }

    @JavascriptInterface
    public void setState(boolean state) {
        this.state = state;
    }
}
